import { createSlice } from "@reduxjs/toolkit";
import { request } from "@/utils";


const userStore=createSlice({
    name:"user",
    initialState:{
        token:localStorage.getItem('token_key')||''
    },
    reducers:{
        setToken(state,action){
            state.token=action.payload
            localStorage.setItem('token_key',action.payload)
        }
    }
})
// 解构出actionCreater
const {setToken}=userStore.actions

const fetchLogin=(loginForm)=>{
    return async (dispatch)=>{
        const res=await request.post('/authorizations',loginForm)
        dispatch(setToken(res.data.token))
    }
}

// 获取reducer函数
const userReducer=userStore.reducer

export {setToken,fetchLogin}
export default userReducer